package com.yx.ecglineview;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.yx.ecglineview.customview.EcgView;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    //-----------心电图↓-----------------
    private EcgView ecg_view;
    private int ecgHeight = 300;//设置心电图布局高度为300dp
    private ArrayList<Integer> data1 = new ArrayList<>();
    private ArrayList<Integer> data2 = new ArrayList<>();
    private int num = 0;//重复次数
    //-----------心电图↑-----------------
    //-----------折线图↓-----------------
    private TextView tv_brokenline,tv_brokenline2;
    //-----------折线图↑-----------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //心电图
        ecg_view = (EcgView) findViewById(R.id.ecg_view);
        inItEcgHeight();
        inItEcgData();

        //折线图
        tv_brokenline = (TextView) findViewById(R.id.tv_brokenline);
        tv_brokenline.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(MainActivity.this, BloodDataActivity.class);
                startActivity(in);
            }
        });

        //折线图2
        tv_brokenline2 = findViewById(R.id.tv_brokenline2);
        tv_brokenline2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(MainActivity.this, BloodData2Activity.class);
                startActivity(in);
            }
        });

    }

    //-----------心电图↓-----------------
    //动态设置心电图高度
    private void inItEcgHeight() {
        final float scale = getResources().getDisplayMetrics().density;
        int mHeight = (int) (ecgHeight * scale + 0.5f);//300dp转到Px
        //设置布局高度方式1
        //ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,mHeight);
        //ecg_view.setLayoutParams(params);
        ////设置布局高度方式2
        ecg_view.getLayoutParams().height = mHeight;//动态设置心电图高度
    }

    //心电图数据
    private void inItEcgData() {
        // 生成一组随机数
        final Random random = new Random();
        for (int i = 0; i < 100; i++) {
            int r = random.nextInt(20);
            data1.add(r);
        }
        //第一条线
        ecg_view.setDatas(1, data1, ecgHeight);

        for (int i = 0; i < 100; i++) {
            int r = random.nextInt(60);
            data2.add(r);
        }
        //第二条线
        ecg_view.setDatas(2, data2, ecgHeight);

        //第三条线，改调用动态方法
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                num++;
                if (num < 500) {//重复次数
                    int r = random.nextInt(60);
                    ecg_view.setDatas(r, ecgHeight);
                    new Handler().postDelayed(this, 100);
                }
            }
        }, 200);
    }
    //-----------心电图↑-----------------
}
